<?php

namespace App\Resource\Payment;

use Hyperf\Context\ApplicationContext;
use Hyperf\Resource\Json\JsonResource;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Stringable\Str;

/**
 * @property int $id
 * @property int $user_id 用户ID
 * @property string $order_no 订单号码
 * @property string $no 单号
 * @property string $amount 金额
 * @property array $request_body 请求信息
 * @property array $response_body 响应信息
 * @property array $notify_body 回调信息
 * @property string $process_at 处理时间
 * @property int $type 支付类型|{"1":"订单支付","2":"订单退款"}
 * @property string $code 支付编码
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $deleted_at
 */
class PaymentResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array
     */
    public function toArray(): array
    {
        return parent::toArray();
    }

    /**
     * Transform the resource into an array for refund.
     * @param string $payCode
     * @return array
     */
    public function toRefundArray(array $input): array
    {
        $action = 'toRefund'.Str::ucfirst($this->code).'Array';

        return $this->{$action}($input);
    }

    /**
     * Transform the resource into an array for safepayMini.
     * @param string $refundNo
     * @param array $input
     * @return array
     */
    protected function toRefundSafepayMiniArray(array $input): array
    {
        return [
            'refund_no' => $this->no,
            'pay_order_no' => $this->order->paid_no,
            'remark' => $input['user_note'],
            'refund_amount' => $this->amount
        ];
    }

    /**
     * Transform the resource into an array for sandpayMini.
     * @param string $refundNo
     * @param array $input
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    protected function toRefundSandpayMiniArray(array $input): array
    {
        return [
            'outReqTime' => date('YmdHis'),
            'outOrderNo' => $this->no,
            'onOutOrderNo' => $this->order->paid_no,
            'amount' => $this->amount,
            'notifyUrl' => getUrl('publics/v1/refund/notify/sandpay/mini'),
            'refundReason' => $input['user_note']
        ];
    }

    /**
     * Transform the resource into an array for neopayMini.
     * @param array $input
     * @return array
     */
    protected function toRefundNeopayMiniArray(array $input): array
    {
        return [
            'businessType' => 'zwrefund',
            'originalSourceTrace' => $this->order->paid_no,
            'originalTransactionType' => 'WAP',
            'tradeamt' => $this->amount,
        ];
    }

    /**
     * Transform the resource into an array for wechatMini.
     * @param array $input
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    protected function toRefundWechatMiniArray(array $input): array
    {
        logs($this->order);
        logs($this->order->paid_amount);
        return [
            'out_trade_no' => $this->order->paid_no,
            'notify_url' => getUrl("publics/v1/refund/notify/wechat/mini/{$this->order->shop_id}"),
            'out_refund_no' => $this->no,
            'amount' => [
                'refund' => intval($this->amount * 100),
                'total' => intval($this->order->paid_amount * 100),
                'currency' => 'CNY',
            ]
        ];
    }

    /**
     * 统统付
     * @param array $input
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    protected function toRefundSumpayMiniArray(array $input): array
    {
        return [
            'refund_no' => $this->no,
            'order_no' => $this->order->merchant_no,
            'refund_amt' => $this->amount,
            'notify_url' => getDomain(). "/publics/v1/refund/notify/sumpay/mini/{$this->order->shop_id}",
        ];
    }

    /**
     * 汇付退款
     * @param array $input
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    protected function toRefundChinapnrpayMiniArray(array $input): array
    {
        return [
            'bgUrl' => getDomain(). "/publics/v1/refund/notify/chinapnrpay/mini/{$this->order->shop_id}",
            'terminalId' => '0010001',
            'refundOrderId' => $this->no,
            'originalOrderId' => $this->order->merchant_no,
            'refundCurrency' => 'CNY',
            'refundAmount' => intval($this->amount * 100)
        ];
    }

    /**
     * Transform the resource into an array for refund.
     * @param string $payCode
     * @return array
     */
    public function toPushOrderArray(array $input=[]): array
    {
        $action = 'toPushOrder'.Str::ucfirst($this->code).'Array';

        return $this->{$action}($input);
    }

    /**
     * Transform the resource into an array for safepayMini.
     * @param array $input
     * @return array
     */
    protected function toPushOrderSafepayMiniArray(array $input): array
    {
        $order = $this->order;
        return [
            'payTransactionNo' => $order->paid_no,
            'amount' => $order->paid_amount,
            'payerCardNo' => $order->payer_no,
            'payerName' => $order->payer_name,
            'orderNo' => $order->paid_no,
            'payTime' => $order->paid_at,
            'payerPhone' => $order->payer_mobile,
            'planPushCustomCode' => 'SH'
        ];
    }

    /**
     * Transform the resource into an array for sandpayMini.
     * @param array $input
     * @return array
     */
    protected function toPushOrderSandpayMiniArray(array $input): array
    {
        $order = $this->order;
        return [
            'customerOrderNo' => $order->paid_no,
            'customCode' => '300106',
            'userInfo' => [
                'bsUserName' => $order->payer_name,
                'bsUserMobile' => $order->payer_mobile,
                'bsUserIdType' => '1',
                'bsUserIdNo' => $order->payer_no,
            ],
            'payInfo' => [
                'bsAppType' => 1,
                'bsOrderNo' => $order->no,
                'bsPayNo' => $order->transaction_no,
                'bsPayTime' => date('YmdHis', strtotime($this->process_at)),
                'bsPayAmt' => str_pad($this->amount * 100, 12, '0', STR_PAD_LEFT),
                'bsPayCury' => '142',
                'bsPayComCode' => '310496099G',
                'bsPayComName' => '杉德支付网络服务发展有限公司'
            ]
        ];
    }

    /**
     * Transform the resource into an array for neopayMini.
     * @param array $input
     * @return array
     */
    protected function toPushOrderNeopayMiniArray(array $input): array
    {
        $order = $this->order;

        return [
            'payTransactionNo' => $order->transaction_id,
            'orderNo' => $this->no,
            'customType' => 'C',
            'amount' => $this->amount,
            'payTime' => $this->process_at,
            'payerCardNo' => $order->payer_no,
            'payerName' => $order->payer_name,
            'payerPhone' => $order->payer_mobile,
            'isBondedMode' => 'Y',
            'planPushCustomCode' => 'SH'
        ];
    }

    /**
     * Transform the resource into an array for wechatMini.
     * @param array $input
     * @return array
     */
    protected function toPushOrderWechatMiniArray(array $input): array
    {
        return [
            'out_trade_no' => $this->order->paid_no,
            'transaction_id' => $this->order->transaction_id
        ];
    }

    /**
     * Transform the resource into an array for sumpayMini.
     * @param array $input
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    protected function toPushOrderSumpayMiniArray(array $input): array
    {
        $order = $this->order;

        $container = ApplicationContext::getContainer();
        $generator = $container->get(IdGeneratorInterface::class);
        return [
            'order_id' => $generator->generate(),
            'trade_no' => $this->order->paid_no,
            'order_no' => $this->order->paid_no,
            'submit_time' => date('YmdHis'),
            'currcode' => 'CNY',
            'pay_amount' => $this->amount,
            'payer_name' => $order->payer_name,
            'payer_type' => '1',
            'payer_number' => $order->payer_no,
            'remark' => $this->order->paid_no.'支付报关'
        ];
    }

    /**
     * Transform the resource into an array for chinapnrpayMini.
     * @param array $input
     * @return array
     */
    protected function toPushOrderChinapnrpayMiniArray(array $input): array
    {
        return [
            'dealId' => $this->order->paid_no,
            'payerIdType' => '1',
            'payerName' => $this->order->payer_name,
            'payerIdNumber' => $this->order->payer_no,
            'orderId' => $this->order->merchant_no,
            'orderCurrency' => 'CNY',
            'orderAmt' => intval($this->amount * 100),
            'freightAmt' => 0,
            'goodsAmt' => intval($this->amount * 100),
            'taxAmt' => 0,
            'offsetAmt' => 0,
            'ecpShortName' => 'SHHF',
            'ecpDomainName' => 'https://mall.youngbuy.com',
            'competCustom' => '1',
            'iaqInstCode' => '1',
            'bgUrl' => getUrl("publics/v1/declares/notify/chinapnrpay/mini/{$this->order->shop_id}"),
            'terminalId' => '0010001'
        ];
    }
}
